import * as echarts from "echarts";
export function barItem(xdata,ydata,ele) {
    const option = {
    dataZoom: [
      {
        type: 'inside'
      }],
    tooltip: {
        trigger: 'axis',
　      axisPointer: {
　　　      type: 'line', 
　　　      lineStyle: {
　　　　　    color: 'rgb(248,175,18)'
　　　      }
　　    },     
    },
    legend: {
        data: ['仓库库存']
    },
    xAxis: {
        data: xdata
    },
    yAxis: {},
    series: [{
        name: '仓库库存',
        type: 'bar',
        showBackground: true,
        itemStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            { offset: 0, color: '#83bff6' },
            { offset: 0.5, color: '#188df0' },
            { offset: 1, color: '#188df0' }
          ])
        },
        data: ydata,
    }]
    };
    const myChart = echarts.init(document.getElementById(ele));
    myChart.setOption(option)
    window.addEventListener("resize", () => {
        myChart.resize();
    });

  }

export function Arrayobj(Array1,Array2){
    const arr = Array1.map((value, i) => ({ value, name: Array2[i] }))
    var obj={}
    var newarr =[]
    arr.map(item=>{
        if(obj[item.name]){
            newarr.map(node=>{
                if(item.name==node.name){
                    node.value += item.value
                }
            })
        }
        else{
            obj[item.name]=1
            newarr.push(item)
        }

    });
     return newarr
}


export function obj(arr){
    var obj={}
    var newarr =[]
    arr.map(item=>{
        if(obj[item.itemId]){
            newarr.map(node=>{
                if(item.itemId==node.itemId){
                    node.itemNum += item.itemNum
                }
            })
        }
        else{
            obj[item.itemId]=1
            newarr.push(item)
        }

    });
     return newarr
}

export function objName(arr){
    var obj={}
    var newarr =[]
    arr.map(item=>{
        if(obj[item.itemName]){
            newarr.map(node=>{
                if(item.itemName==node.itemName){
                    node.itemNum += item.itemNum
                }
            })
        }
        else{
            obj[item.itemName]=1
            newarr.push(item)
        }

    });
     return newarr
}

export function pieItem(data,ele){
    const myChart = echarts.init(document.getElementById(ele));
        const option = {
            title: {              
              subtext: "类型统计",
              left: 'center'
            },
            tooltip: {
              trigger: 'item',
              formatter: '{a} <br/>{b} : {c} ({d}%)'
            },
            legend: {    orient: 'vertical',    left: 'left'  },
            series: [
              {
                name: '类型',
                type: 'pie',
                radius: ['50%', '70%'],
                avoidLabelOverlap: false,
                itemStyle: {
                  borderRadius: 20,
                  borderColor: '#fff',
                  borderWidth: 2
                },
                label: {
                  show: false,
                  position: 'center'
                },
                emphasis: {
                  label: {
                    show: true,
                    fontSize: 15,
                    fontWeight: 'bold'
                  }
                },
                labelLine: {
                  show: false
                },
                data: data,
              }
            ]
          };

    // option && myChart.setOption(option);
    myChart.setOption(option)
    window.addEventListener("resize", () => {
        myChart.resize();
    });
}

export function lineOperate(xdata,ydata,ele){
    const option = {
        // title: {
        //     text: '损耗分析'
        // },
        dataZoom: [
          {
            type: 'inside'
          }],
        tooltip: {
          trigger: 'axis',
　      axisPointer: {
　　　      type: 'line', 
　　　      lineStyle: {
　　　　　    color: 'rgb(248,175,18)'
　　　      }
　　    }, 
        },
        legend: {
            data: ['损耗量']
        },
        xAxis: {
            type: 'category',
            data: xdata
          },
          yAxis: {
            type: 'value'
          },
          series: [
            {
              name: '损耗量',
              data: ydata,
              type: 'line',
              smooth: true
            }
          ]

        };
        const myChart = echarts.init(document.getElementById(ele));
        myChart.setOption(option)
        window.addEventListener("resize", () => {
            myChart.resize();
        });
}

export function barOut(xdata,ydata,ele) {
    const option = {
    // title: {
    //     text: '使用频率分析'
    // },
    dataZoom: [
      {
        type: 'inside'
      }],
    tooltip: {},
    legend: {
        data: ['使用量']
    },
    xAxis: {
        data: xdata
    },
    yAxis: {},
    series: [{
        name: '使用量',
        type: 'bar',
        data: ydata,
        itemStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            { offset: 0, color: '#61a0a8' },
            { offset: 0.5, color: '	#40B5AD' },
            { offset: 1, color: '	#40B5AD' }
          ])
        },
        color:['#91c7ae']
    }]
    };
    const myChart = echarts.init(document.getElementById(ele));
    myChart.setOption(option)
    window.addEventListener("resize", () => {
        myChart.resize();
    });

  }

